import java.util.Arrays;
import java.util.Scanner;

class ListNode {
    int val;
    ListNode next;
    ListNode(int x) { val = x; }
}

public class GroupSort {

    public static ListNode groupSort(ListNode head, int n) {
        if (head == null || n <= 1) return head;

        ListNode dummy = new ListNode(-1);
        dummy.next = head;
        ListNode prevGroupEnd = dummy;

        while (prevGroupEnd.next != null) {
            // groupStart 和 groupEnd 分别表示一个子链表（待排序）的头节点和尾节点
            ListNode groupStart = prevGroupEnd.next;
            ListNode groupEnd = getGroupEnd(groupStart, n);

            if (groupEnd == null) break;

            // 保存链表的笑一个子链表的头节点
            ListNode nextGroupStart = groupEnd.next;
            // 将子链表的尾部设为 null，方便后续对子链表进行归并排序
            groupEnd.next = null;

            // 返回排序完后的子链表的头节点
            groupStart = sortList(groupStart);
            // 这里需要将排序完后的子链表的 next 指向下一个子链表的头节点上
            // 需要主要的是排序完后的子链表尾节点可能已经变了
            groupEnd = getGroupEnd(groupStart, n);
            groupEnd.next = nextGroupStart;

            prevGroupEnd = groupEnd;
            if (dummy.val == -1) {
                dummy.next = groupStart;
                dummy.val = 0;
            }
        }

        return dummy.next;
    }

    private static ListNode getGroupEnd(ListNode start, int n) {
        ListNode end = start;
        for (int i = 1; i < n && end != null; i++) {
            end = end.next;
        }
        return end;
    }

    private static ListNode sortList(ListNode head) {
        if (head == null || head.next == null) return head;

        ListNode mid = getMid(head);
        ListNode left = head;
        ListNode right = mid.next;
        mid.next = null;

        // 递归排序左右两部分链表
        ListNode sortedLeft = sortList(left);
        ListNode sortedRight = sortList(right);

        return merge(sortedLeft, sortedRight);
    }

    private static ListNode getMid(ListNode head) {
        ListNode slow = head, fast = head.next;
        while (fast != null && fast.next != null) {
            slow = slow.next;
            fast = fast.next.next;
        }
        return slow;
    }

    private static ListNode merge(ListNode l1, ListNode l2) {
        ListNode dummy = new ListNode(0);
        ListNode current = dummy;

        while (l1 != null && l2 != null) {
            if (l1.val > l2.val) {
                current.next = l1;
                l1 = l1.next;
            } else {
                current.next = l2;
                l2 = l2.next;
            }
            current = current.next;
        }

        if (l1 != null) current.next = l1;
        if (l2 != null) current.next = l2;

        return dummy.next;
    }

    public static void printList(ListNode head) {
        ListNode current = head;
        System.out.print("{");
        while (current.next != null) {
            System.out.print(current.val + ",");
            current = current.next;
        }
        System.out.print(current.val);
        System.out.println("}");
    }

    public static ListNode buildList(int[] values) {
        if (values == null || values.length == 0) return null;
        ListNode head = new ListNode(values[0]);
        ListNode current = head;
        for (int i = 1; i < values.length; i++) {
            current.next = new ListNode(values[i]);
            current = current.next;
        }
        return head;
    }

    public static int[] parseInput(String input) {
        String trimmed = input.replaceAll("[{}]", "").trim();
        String[] parts = trimmed.split(",");

        int[] values = new int[parts.length];
        for (int i = 0; i < parts.length; i++) {
            values[i] = Integer.parseInt(parts[i].trim());
        }
        return values;
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        // {6,1,4,3},2
        // {1,3,4},4
        // {1,2,3,4},3
        // {1,2,3,4},1
        String input = scanner.nextLine();

        int[] values = parseInput(input);
        int[] array = Arrays.copyOfRange(values, 0, values.length - 1);
        ListNode head = buildList(array);
        int len = values[values.length - 1];

        head = groupSort(head, len);

        printList(head);

        scanner.close();
    }
}
